package com.hd.crdao.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.MyOrderAdapter;
import com.hd.crdao.bean.MyOrderBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.view.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/6/6 0006
 */

public class MyOrderBaseFragment extends Fragment {


    MyListView mListview;
    ProgressBar mProgreassbar;
    TextView mLoadState;
    LinearLayout mProgreassbarLayout;
    public Handler mHandler = new Handler();
    public MyOrderAdapter mMyOrderAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mListview = (MyListView) view.findViewById(R.id.myorderlist);
        mLoadState = (TextView) view.findViewById(R.id.load_state);
        mProgreassbar = (ProgressBar) view.findViewById(R.id.progreassbar);
        mProgreassbarLayout = (LinearLayout) view.findViewById(R.id.progreassbar_layout);
        initData();
        mListview.setonRefreshListener(new MyListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Tasks(), 2000);
            }
        });
    }

    /**
     * 初始化数据
     */
    private void initData() {
        String businessid = SPUtils.getString(Config.businessId, "");
        String state = getArguments().getString("state");
        // String businessid ="071c3e6096d34ffd8269d16402825a31";
        OkHttpUtils.post()
                .addParams("data", businessid + "~" + state)
                .addParams("sign", Constants.SIGN)
                .url(Constants.NEWCARLORDERIST_URL)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call request, Exception e, int id) {
                MyToast.show("网络开了个小差");
                mProgreassbarLayout.setVisibility(View.GONE);
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                Gson gson = new Gson();
                MyOrderBean myOrderBean = gson.fromJson(response, MyOrderBean.class);

                if (myOrderBean == null) {
                    return;
                }
                if ("00".equals(myOrderBean.getSuccess())) {
                    //设置适配器数据

                    mProgreassbarLayout.setVisibility(View.GONE);
                    List<MyOrderBean.RowsBean> rows = myOrderBean.getRows();
                    mMyOrderAdapter = new MyOrderAdapter(rows);
                    mListview.setAdapter(mMyOrderAdapter);
                    if (rows.size() == 0) {
                        mProgreassbarLayout.setVisibility(View.VISIBLE);
                        mProgreassbar.setVisibility(View.GONE);
                        mLoadState.setText("无数据");
                    }
                } else {
                    mProgreassbarLayout.setVisibility(View.VISIBLE);
                    mLoadState.setText(myOrderBean.getMessage());
                }
            }
        });


    }



  /*  protected abstract String setStatus();*/


    private class Tasks implements Runnable {
        @Override
        public void run() {
            initData();
            mMyOrderAdapter.notifyDataSetChanged();
            mListview.onRefreshComplete();
        }
    }
}
